package main

import (
	"gitee.com/general252/pdh"
	"gitee.com/general252/pdh/core"
	"log"
	"os"
	"time"
)

func main() {
	testPerformance()

	//testPDH()
}

func testPerformance() {
	const appName = "processor_demo"

	var names []*pdh.CounterName
	if false {
		names = pdh.NewCounterNameBuilder().
			WithCPU().
			WithProcessCPU(appName).
			WithProcessMemory(appName).
			WithProcessHandleCount(appName).
			WithProcessIOWriteBytesPerSec(appName).
			WithProcessIOReadBytesPerSec(appName).
			Build()
	}

	performance := pdh.NewPerformance()
	performance.AddCounter(names...)

	err := performance.Open(
		func(data []*pdh.ObjectCounterValue) {
			log.Println()
			for _, item := range data {
				log.Printf("%30v %20v %-30v [%-10v] [%30v]",
					item.LastTime.Format(time.RFC3339),
					item.ValueString(),
					item.Type, item.Param, item.Field)
			}
		},
		func(ins *pdh.Performance) {
			builder := pdh.NewCounterNameBuilder()

			if false {
				// 网卡
				if items, err := ins.GetQuery().EnumObjectItems(string(pdh.CounterTypeNetworkInterface)); err != nil {
					log.Println(err)
				} else {
					for _, item := range items {
						if item == pdh.Total {
							continue
						}
						builder.WithNetworkInterfaceBytesReceivedPerSec(item)
						builder.WithNetworkInterfaceBytesSentPerSec(item)
						builder.WithNetworkInterfaceCurrentBandwidth(item)
					}
				}
			}

			if true {
				// 硬盘
				if items, err := ins.GetQuery().EnumObjectItems(string(pdh.CounterTypePhysicalDisk)); err != nil {
					log.Println(err)
				} else {
					for _, item := range items {
						if item == pdh.Total {
							continue
						}
						builder.WithPhysicalDiskDiskTime(item)
						builder.WithPhysicalDiskAvgDiskBytesPerRead(item)
						builder.WithPhysicalDiskAvgDiskBytesPerWrite(item)
						builder.WithPhysicalDiskAvgDiskSecPerTransfer(item)
						builder.WithPhysicalDiskDiskTransfersPerSec(item)
					}
				}
			}

			ins.AddCounter(builder.Build()...)
		})
	if err != nil {
		log.Println(err)
		return
	}

	defer performance.Close()

	pdh.WaitForAppQuitSignal(true, func(sign os.Signal) {
	})
}

func testPDH() {
	// 性能监视器数据
	query := pdh.NewQuery()
	if err := query.Open(); err != nil {
		log.Println(err)
		return
	}

	defer query.Close()

	if objects, err := query.EnumObject(); err != nil {
		log.Println(err)
		return
	} else {
		for idx, object := range objects {
			log.Printf("%v %v", idx, object)
		}
	}

	if instances, err := query.EnumObjectItems(string(pdh.CounterTypeProcessor)); err != nil {
		log.Println(err)
		return
	} else {
		for idx, instance := range instances {
			log.Printf("%v %v", idx, instance)
		}
	}

	if instances, err := query.EnumObjectItems(string(pdh.CounterTypePhysicalDisk)); err != nil {
		log.Println(err)
	} else {
		for idx, instance := range instances {
			log.Printf("%v %v", idx, instance)
		}
	}

	var (
		err        error
		cpuCounter core.PDH_HCOUNTER
		memCounter core.PDH_HCOUNTER
	)

	if cpuCounter, err = query.AddCounter("\\Processor Information(_Total)\\% Processor Utility"); err != nil {
		log.Println(err)
		return
	}
	if memCounter, err = query.AddCounter("\\Memory\\Available MBytes"); err != nil {
		log.Println(err)
		return
	}

	_ = query.CollectData()
	for {
		time.Sleep(time.Second)
		_ = query.CollectData()

		cpuValue, _ := query.GetFormattedCounterValueDouble(cpuCounter)
		memValue, _ := query.GetFormattedCounterValueDouble(memCounter)

		log.Printf("%4.2v%% %vMB", cpuValue, memValue)
	}
}
